package com.icim.twqc.importer.job;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.icim.core.pojo.FtpConfigure;
import com.icim.core.service.IFtpTwqcService;
import com.icim.twqc.importer.service.IRetXmlImportService;
import com.icim.util.FileUtil;


public class TwqcImportJob {

	private static final Logger logger = LoggerFactory.getLogger(TwqcImportJob.class);
	private IFtpTwqcService ftpTwqcService;
	private IRetXmlImportService retXmlImportTwqcService;
	
	public void setftpTwqcService(IFtpTwqcService ftpTwqcService) {
        this.ftpTwqcService = ftpTwqcService;
    }
	
	public IRetXmlImportService getRetXmlImportTwqcService() {
		return retXmlImportTwqcService;
	}

	public void setRetXmlImportTwqcService(
			IRetXmlImportService retXmlImportTwqcService) {
		this.retXmlImportTwqcService = retXmlImportTwqcService;
	}

	public void subMainProc() {
		long firstTime = System.currentTimeMillis();
		long beginTime = System.currentTimeMillis();

        FtpConfigure ftpConfigure = ftpTwqcService.getFtpConfigure();
        String localTempPath = ftpConfigure.getLocalTemporaryPath();
        String LocalZipBakPath = ftpConfigure.getLocalZipBakPath();
        
        logger.info("twqc importer job starting ......");
        
       
        String gzFileName = null;
        try {
            // 连接FTP
        	ftpTwqcService.connect();
          
          File[] xmlFiles = FileUtil.listFiles(localTempPath);
          for (File xmlFile : xmlFiles) {
            try {
            	retXmlImportTwqcService.importXMLProc(xmlFile);
              // 将文件从原始路径 移动到OK路径
              ftpTwqcService.uploadFile(xmlFile,ftpConfigure.getFtpOkPath());

            } catch (Exception ex) {
                logger.error(ex.toString());
                // 将文件从原始路径 移动到NG路径
                ftpTwqcService.uploadFile(xmlFile,ftpConfigure.getFtpNgPath());
                FileUtil.removeFile(xmlFile.getName(), localTempPath, LocalZipBakPath);
                // TODO
                sendAlarm();
            } finally {
              //FileUtil.removeFile(fileName, localTempPath, localGzPath);
            }
          }
          FileUtil.deleteAllFiles(localTempPath, FileUtil.XML);
          ftpTwqcService.disconnectFtp();
          //logger.warn(" TwqcImportJob first connect ftp costs : " + (System.currentTimeMillis() - beginTime));
          
          beginTime = System.currentTimeMillis();

          // 连接FTP
          ftpTwqcService.connect();
            // 获取文件列表
            List<String> fileNameList = ftpTwqcService.listFiles(FileUtil.XML);

            // 遍历GZ文件
            for (String fileName : fileNameList) {

                gzFileName = fileName;
                logger.info("find file [{}]", fileName);

                // 下载GZ文件到本地
                ftpTwqcService.downLoadFile(fileName);
                String localGzFileName = localTempPath + "/" + fileName;

                try {
                    File xmlFile = new File(localGzFileName);
                    
                    //importXMLProc(xmlFile);
                    retXmlImportTwqcService.importXMLProc(xmlFile);
                    
                    // 将文件从原始路径 移动到OK路径
                    ftpTwqcService.removeFiles(xmlFile.getName(), true);

                } catch (Exception ex) {
                    logger.error(ex.toString());
                    if (!StringUtils.isEmpty(gzFileName)) {
                        // 将文件从原始路径 移动到NG路径
                    	ftpTwqcService.removeFiles(gzFileName, false);
                    	FileUtil.removeFile(gzFileName, localTempPath, LocalZipBakPath);
                    }
                    // TODO
                    sendAlarm();

                } finally {

                  //FileUtil.removeFile(fileName, localTempPath, localGzPath);
                }
            }
            
            //logger.warn(" TwqcImportJob second connect ftp costs : " + (System.currentTimeMillis() - beginTime));
            
        } catch (Exception e) {
            sendAlarm();
            logger.error(e.toString());
        } finally {
            // 断开连接
            try {
              FileUtil.deleteAllFiles(localTempPath, FileUtil.XML);
              ftpTwqcService.disconnectFtp();
            } catch (IOException e) {
                logger.error(e.toString());
            }
        }
        
        //logger.warn(" TwqcImportJob run all method costs : " + (System.currentTimeMillis() - firstTime));
    }
	private void sendAlarm() {
        // TODO Auto-generated method stub
		logger.info("Alarm:import twqc xmlFile fail.....");
    }
}
